package com.itcast.netty.utils;

import com.itcast.common.entity.result.ResponseResult;
import com.itcast.common.enums.ResponseEnum;

/**
 * 返回结果工具类
 *
 * @author zheng.zhang
 */
public class ResponseResultUtil {

    /**
     * 请求失败
     *
     * @return 响应数据
     */
    public static ResponseResult<?> error(ResponseEnum responseEnum) {
        return ResponseResult.builder()
                .code(responseEnum.getCode())
                .message(responseEnum.getMsg())
                .data(null)
                .build();
    }

    /**
     * 请求成功
     *
     * @return 响应数据
     */
    public static ResponseResult<?> success() {
        return success(null);
    }

    /**
     * 请求成功
     *
     * @param data 响应数据
     * @return 响应结果
     */
    public static ResponseResult<?> success(Object data) {
        return ResponseResult.builder()
                .code(ResponseEnum.REQUEST_SUCCESS.getCode())
                .message(ResponseEnum.REQUEST_SUCCESS.getMsg())
                .data(data)
                .build();
    }

    /**
     * 判断是否成功
     *
     * @param responseResult 响应结果
     * @return 是否成功
     */
    public static boolean judgementSuccess(ResponseResult<?> responseResult) {
        return responseResult.getCode().equals(ResponseEnum.REQUEST_SUCCESS.getCode());
    }
}
